package com.example.administrator.befoolishdragsortrecyclerview;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static android.support.v7.widget.helper.ItemTouchHelper.ACTION_STATE_DRAG;
import static android.support.v7.widget.helper.ItemTouchHelper.DOWN;
import static android.support.v7.widget.helper.ItemTouchHelper.UP;

public class MainActivity extends AppCompatActivity {


    MyRecyclerView rv;
    List<String> list = new ArrayList<>();
    private RecyclerView.Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for (int i = 0; i < 40; i++) {
            list.add("项目w- " + i);
        }

        rv = (MyRecyclerView) findViewById(R.id.rv);
        ItemTouchHelper.Callback callback = new ItemTouchHelper.Callback() {
            @Override
            public boolean isLongPressDragEnabled() {
                return false;//super.isLongPressDragEnabled();
            }

            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                return makeMovementFlags(UP | DOWN, 0);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                                  RecyclerView.ViewHolder target) {

                adapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());

                Collections.swap(list, viewHolder.getAdapterPosition(), target.getAdapterPosition());

                return true;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

            }

            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);
                viewHolder.itemView.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
                rv.setOnDraging(false);//拖拽结束标志   方便进行滑动冲突处理
            }

            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                super.onSelectedChanged(viewHolder, actionState);
                if (actionState == ACTION_STATE_DRAG) {
                    rv.setOnDraging(true);;//拖拽开始标志   方便进行滑动冲突处理
                    viewHolder.itemView.setBackgroundColor(Color.parseColor("#44666666"));
                }
            }
        };
        final ItemTouchHelper helper = new ItemTouchHelper(callback);


        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setNestedScrollingEnabled(false);//这一步很重要   取消嵌套滚动
        adapter = new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, parent, false);
                return new RecyclerView.ViewHolder(view) {
                };
            }

            @Override
            public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
                ((TextView) holder.itemView.findViewById(R.id.item))
                        .setText(list.get(position));
                holder.itemView.findViewById(R.id.iv)
                        .setOnTouchListener(new View.OnTouchListener() {
                            public boolean onTouch(View v, MotionEvent event) {
                                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                                    helper.startDrag(holder);
                                }
                                return false;
                            }
                        });
            }

            @Override
            public int getItemCount() {
                return list.size();
            }
        };
        rv.setAdapter(adapter);


        helper.attachToRecyclerView(rv);

    }
}
